<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login and Register</title>
    <link rel="stylesheet" href="/css/styles.css">
    <script src="/js/axios.min.js"></script>
    <script src="/js/vue.min.js"></script>
</head>
<body>

<div id="app" class="form-container">
    <div v-if="showLogin">
        <h2>Login</h2>
        <form @submit.prevent="login">
            <div class="form-group">
                <label for="loginAccount">Account:</label>
                <input type="text" v-model="loginData.account" required>
            </div>
            <div class="form-group">
                <label for="loginPassword">Password:</label>
                <input type="password" v-model="loginData.password" required>
            </div>
            <button type="submit">Login</button>
        </form>
        <button class="toggle-button" @click="toggleForm">Don't have an account? Register</button>
    </div>

    <div v-else>
        <h2>Register</h2>
        <form @submit.prevent="register">
            <div class="form-group">
                <label for="registerAccount">Account:</label>
                <input type="text" v-model="registerData.account" required>
            </div>
            <div class="form-group">
                <label for="registerPassword">Password:</label>
                <input type="password" v-model="registerData.password" required>
            </div>
            <div class="form-group">
                <label for="registerEmail">Email:</label>
                <input type="email" v-model="registerData.email" required>
            </div>
            <div class="form-group">
                <label for="registerSexy">Gender:</label>
                <input type="text" v-model="registerData.sexy" required>
            </div>
            <div class="form-group">
                <label for="registerIntroduce">Introduce:</label>
                <input type="text" v-model="registerData.introduce">
            </div>
            <button type="submit">Register</button>
        </form>
        <button class="toggle-button" @click="toggleForm">Already have an account? Login</button>
    </div>
</div>

<script>
    new Vue({
        el: '#app',
        data: {
            showLogin: true,
            loginData: {
                account: '',
                password: ''
            },
            registerData: {
                account: '',
                password: '',
                email: '',
                sexy: '',
                introduce: ''
            }
        },
        methods: {
            toggleForm() {
                this.showLogin = !this.showLogin;
            },
            login() {
                axios.post('/sm/users/login', new URLSearchParams(this.loginData))
                    .then(response => {
                        if (response.data) {
                            alert('Login successful!');
                        } else {
                            alert('Invalid username or password.');
                        }
                    })
                    .catch(error => console.error('Error:', error));
            },
            register() {
                axios.post('/sm/users/register', new URLSearchParams(this.registerData))
                    .then(response => {
                        if (response.data) {
                            alert('Registration successful!');
                        } else {
                            alert('User already exists.');
                        }
                    })
                    .catch(error => console.error('Error:', error));
            }
        }
    });
</script>

</body>
</html>